package com.cf.game.cowboy.handler;

import com.cf.game.Player;
import com.cf.game.cowboy.CowBoyGame;
import com.cf.game.cowboy.MessageHandler;
import com.cf.game.cowboy.msg.CowUserInfoRequest;
import com.cf.game.cowboy.msg.CowUserInfoResponse;
import com.cf.game.service.constants.EnumReturnCode;
import com.cf.game.service.pojo.UserInfo;
import com.cf.game.service.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * cf-game-StakeMessageHandler
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/3/27 14:34
 */
@Slf4j
@Component
public class UserInfoMessageHandler implements MessageHandler<CowUserInfoRequest, CowBoyGame> {

    @Autowired
    private UserInfoService userInfoService;

    @Override
    @Transactional
    public void handle(Player player, CowBoyGame game, CowUserInfoRequest message) {
        //单播用户余额消息
        CowUserInfoResponse userInfoResponse = new CowUserInfoResponse("获取余额OK", EnumReturnCode.SUCCESS.getCode());
        UserInfo userInfo = userInfoService.getUserInfoByUserId(player.getUserInfo().getId());
        if (userInfo != null) {
            userInfoResponse.setMoney(userInfo.getGameCoin() == null ? 0 : userInfo.getGameCoin());
            player.unicast(userInfoResponse);
        }

    }

    public boolean support(Class clazz) {
        return CowUserInfoRequest.class.equals(clazz);
    }


}
